package com.hpe.fastfood.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hpe.fastfood.pojo.Comment;
import com.hpe.fastfood.pojo.User;
import com.hpe.fastfood.service.LoginService;
import com.hpe.fastfood.service.UserService;
import com.hpe.fastfood.util.Page;

/**
 * @ClassName: UserController
 * @Description:TODO 用户Controller
 * @author: sunwen
 * @date: 2018年11月25日 下午3:11:08
 */
@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * @Description:TODO用户个人界面
	 * @author:sunwen
	 * @date: 2018年11月25日 下午3:14:16
	 * @return
	 */
	@RequestMapping(value = "toUser")
	public String toUser() {
		return "user_center";
	}

	/**
	 * @Description:TODO查询用户个人信息
	 * @author:sunwen
	 * @date: 2018年11月25日 下午3:39:35
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "selectUserMag")
	public ModelAndView selectUserMag(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		User user = null;
		try {
			user = (User) req.getSession().getAttribute("loginUser");
			user = userService.selectUserMegById(user.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("user", user);
		mv.setViewName("user_account");
		return mv;
	}

	/**
	 * @Description:TODO更改个人信息
	 * @author:sunwen
	 * @date: 2018年11月25日 下午4:32:05
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "updateUserMag", method = RequestMethod.POST)
	public String updateUserMag(User user) {
		try {
			userService.updateUserMag(user);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "redirect:/user/selectUserMag";
	}

	/**
	 * @Description:TODO查询用户评价
	 * @author:sunwen
	 * @date: 2018年11月25日 下午7:44:02
	 * @param req
	 * @return
	 */
	@RequestMapping("selectCommentByUserId")
	public ModelAndView selectCommentByUserId(Page page, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		User user = null;
		try {
			user = (User) req.getSession().getAttribute("loginUser");
			page = userService.selectCommentByUserId(page, user.getUserId());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		mv.addObject("user", user);
		mv.addObject("page", page);
		mv.setViewName("/user_message");
		return mv;
	}

	/**
	 * @Description:TODO添加留言
	 * @author:sunwen
	 * @date: 2018年11月26日 下午1:55:34
	 * @param comment
	 * @return
	 */
	@RequestMapping(value = "AddComment", method = RequestMethod.POST)
	public String AddComment(Comment comment, HttpServletRequest req) {
		// 调用service
		User user = null;
		try {
			user = (User) req.getSession().getAttribute("loginUser");
			comment.setUserId(user.getUserId());
			userService.addComment(comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 添加成功之后
		// 重定向到
		return "redirect:/shop/selectShopById?foodId=" + comment.getFoodId(); // 重定向
	}
}
